/**
 * The Class HashTable.
 */
public class HashTable {

	/** The size of the table */
	private int TABLE_SIZE;

	/** The table. */
	HashEntry[] table;

	/**
	 * Instantiates a new hash table.
	 * 
	 * @param size
	 *            the size of the table
	 */
	HashTable(int size) {
		// check input
		if (size<2)
		{
			throw new RuntimeException("not enought elements for HashTable");
		}
		// set the table size
		TABLE_SIZE = size;
		table = new HashEntry[TABLE_SIZE];
		// reset with nulls
		for (int i = 0; i < TABLE_SIZE; i++)
			table[i] = null;
	}

	/**
	 * Gets the value for the key.
	 * 
	 * @param key
	 *            the key
	 * @return the value
	 */
	public int get(int key) {
		// get the hash val
		int hash = (key % TABLE_SIZE);
		// find in the table
		while (table[hash] != null && table[hash].getKey() != key)
			hash = (hash + 1) % TABLE_SIZE;
		if (table[hash] == null)
			return -1;
		else
			return table[hash].getValue();
	}

	/**
	 * Put key and value.
	 * 
	 * @param key
	 *            the key
	 * @param value
	 *            the value
	 */
	public void put(int key, int value) {
		// find the hash val
		int hash = (key % TABLE_SIZE);
		while (table[hash] != null && table[hash].getKey() != key)
			hash = (hash + 1) % TABLE_SIZE;
		// set the value
		table[hash] = new HashEntry(key, value);
	}
}